package com.sinosoft.dsp.component.bpm.viewbean.activity.part;

import org.jbpm.xsdobject.ContinueType;

import com.sinosoft.dsp.component.bpm.service.core.BpmSelectItemsService;
import com.sinosoft.dsp.component.bpm.util.ActivityType;
import com.sinosoft.dsp.component.bpm.util.ProcessParse;
import com.sinosoft.dsp.component.bpm.viewbean.BpmJpdlDefineViewBean;
import com.sinosoft.dsp.component.bpm.viewbean.activity.BpmAbstractActivityPartEditViewBean;
import com.sinosoft.dsp.component.bpm.viewbean.activity.IBpmActivityPartEditViewBean;
import com.sinosoft.dsp.system.core.exception.SinoException;
import com.sinosoft.dsp.system.core.log.Log;
import com.sinosoft.dsp.system.to.IGenericTO;
import com.sinosoft.dsp.system.to.attribute.Attribute;
import com.sinosoft.dsp.system.to.helper.ToHelper;
import com.sinosoft.dsp.system.utils.Const;
import com.sinosoft.dsp.system.utils.ReflectionUtils;

public class BpmActivityCommonBaseViewBean extends
		BpmAbstractActivityPartEditViewBean implements
		IBpmActivityPartEditViewBean {

	private static final Log log = Log
			.getLog(BpmActivityCommonBaseViewBean.class.getName());

	// 活动名称
	public static final String FIELD_NAME = "name";
	public static final String FIELD_INIT_NAME = "initName";

	// 活动类型
	public static final String FIELD_TYPE = "type";
	public static final String FIELD_TYPE_STRING = "typeString";

	// 活动描述
	public static final String FIELD_DESC = "description";

	// 活动执行方式
	public static final String FIELD_CONTINUE = "continue";

	@Override
	public void init(IGenericTO tos) throws SinoException {
		super.init(tos);

		// name
		Attribute<?> name = ToHelper.getContentAttriubteByName(tos,
				BPM_NODE_NAME);
		String nameStr = name.convertToString();
		this.addField(FIELD_NAME, nameStr);
		this.addField(FIELD_INIT_NAME, nameStr);

		// type
		BpmJpdlDefineViewBean pvb = (BpmJpdlDefineViewBean) this.getParent()
				.getParent();
		org.jbpm.xsdobject.Process process = pvb.getProcess();
		Object obj = ProcessParse.getActivity(process, nameStr);
		// 如果无法获得对象，则创建一个新的对象
		if (obj == null) {
			Attribute<?> type = ToHelper.getContentAttriubteByName(tos,
					BPM_NODE_TYPE);
			ActivityType at = ActivityType.getActivityType(type
					.convertToString());
			obj = at.createObject(nameStr);
			process.getDescriptionAndSwimlaneAndOn().add(obj);
		}

		// 类型
		ActivityType at = ActivityType.getActivityType(obj.getClass());
		this.addField(FIELD_TYPE, at.getTagName());
		this.addField(FIELD_TYPE_STRING, at.getDisplayName());

		// 初始值
		// continue
		this.addField(FIELD_CONTINUE,
				BpmSelectItemsService.SELECT_ITEM_CONTINUE_DEFAULT);

		// 从活动中赋值
		try {
			ContinueType ct = (ContinueType) ReflectionUtils.invokeGetMethod(
					obj, FIELD_CONTINUE);
			if (ct != null) {
				this.addField(FIELD_CONTINUE, ct.value());
			}
		} catch (Exception e) {
			log.debug("无法获得活动执行方式。");
		}

		try {
			String desc = (String) ReflectionUtils.invokeGetMethod(obj,
					FIELD_DESC);
			this.addField(FIELD_DESC, Const.NVL(desc, ""));
		} catch (Exception e) {
			log.debug("无法获得活动描述方式。");
		}

	}
}
